package com.fangdd.logtrace.async;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.*;

/**
 * Created by sulong on 16-9-7.
 */
public class TraceIdExecutorService implements ExecutorService {
    private ExecutorService inner;

    public TraceIdExecutorService(ExecutorService real) {
        inner = real;
    }

    @Override
    public void shutdown() {
        inner.shutdown();
    }

    @Override
    public List<Runnable> shutdownNow() {
        return inner.shutdownNow();
    }

    @Override
    public boolean isShutdown() {
        return inner.isShutdown();
    }

    @Override
    public boolean isTerminated() {
        return inner.isTerminated();
    }

    @Override
    public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
        return inner.awaitTermination(timeout, unit);
    }

    @Override
    public <T> Future<T> submit(Callable<T> task) {
        return inner.submit(new TraceCallable<T>(task));
    }

    @Override
    public <T> Future<T> submit(Runnable task, T result) {
        return inner.submit(new TraceRunnable(task), result);
    }

    @Override
    public Future<?> submit(Runnable task) {
        return inner.submit(new TraceRunnable(task));
    }

    @Override
    public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException {
        if (tasks == null || tasks.isEmpty()) {
            return inner.invokeAll(tasks);
        }

        List<Callable<T>> newTasks = new ArrayList<Callable<T>>(tasks.size());
        for (Callable<T> callable : tasks) {
            newTasks.add(new TraceCallable<T>(callable));
        }

        return inner.invokeAll(newTasks);
    }

    @Override
    public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException {
        if (tasks == null || tasks.isEmpty()) {
            return inner.invokeAll(tasks, timeout, unit);
        }

        List<Callable<T>> newTasks = new ArrayList<Callable<T>>(tasks.size());
        for (Callable<T> callable : tasks) {
            newTasks.add(new TraceCallable<T>(callable));
        }

        return inner.invokeAll(newTasks, timeout, unit);
    }

    @Override
    public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException {
        if (tasks == null || tasks.isEmpty()) {
            return inner.invokeAny(tasks);
        }

        List<Callable<T>> newTasks = new ArrayList<Callable<T>>(tasks.size());
        for (Callable<T> callable : tasks) {
            newTasks.add(new TraceCallable<T>(callable));
        }

        return inner.invokeAny(newTasks);
    }

    @Override
    public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
        if (tasks == null || tasks.isEmpty()) {
            return inner.invokeAny(tasks, timeout, unit);
        }

        List<Callable<T>> newTasks = new ArrayList<Callable<T>>(tasks.size());
        for (Callable<T> callable : tasks) {
            newTasks.add(new TraceCallable<T>(callable));
        }

        return inner.invokeAny(newTasks, timeout, unit);
    }

    @Override
    public void execute(Runnable command) {
        inner.execute(new TraceRunnable(command));
    }
}
